<?php
declare (strict_types = 1);

namespace app\command\elastic;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Log;
use app\model\shop\Product;
use app\model\vipshop\VipProduct;
use app\elastic\ProductElastic;
use app\elastic\VipProductElastic;

class ProductInit extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('init:product')
             ->setDescription('ES数据初始化:商品');
    }

    protected function execute(Input $input, Output $output)
    {
        $productElastic = new ProductElastic();
        //判断索引名是否存在 不存在创建索引 存在修改索引
        if (!$productElastic->exists()){
            Log::info('索引不存在，准备创建');
            $productElastic->createIndex();
            Log::info('创建成功，准备初始化数据');
            $this->rebuildShopData();
            $this->rebuildVipShopData();
            Log::info('操作成功');
        }



//        $productElastic->updateIndex();
    }

    protected function rebuildShopData()
    {
        $productElastic = new ProductElastic();
        Log::info('准备同步严选商城数据:');
        Product::field('
            goods_id,goods_name,category_id,brand_id,goods_type,
            market_price,price,shipping_fee,clicks,sales,evaluates,
            keywords,state,sort,picture,img_id_array,
            description,create_time,update_time,goods_spec_format
        ')
        ->append(['doc_id','data_source'])
        ->with(['skus' => function($query){
            $query->field('sku_id,goods_id,sku_name,attr_value_items,market_price,price,stock');
        }])
        ->chunk(100, function ($products) use($productElastic){
            if(!$products->count()){
                Log::error('同步失败');
                exit('同步失败');
            }
            Log::info(sprintf('正在同步 ID 范围为 %s 至 %s 的商品', $products->first()->goods_id, $products->last()->goods_id));
            $productElastic->bulkDoc($products->toArray());
            Log::info(sprintf('ID 范围为 %s 至 %s 的商品同步成功', $products->first()->goods_id, $products->last()->goods_id));
        });
        Log::info('同步严选商城数据结束');
    }

    protected function rebuildVipShopData()
    {
        $productElastic = new ProductElastic();
        Log::info('准备同步VIP商城数据:');

        $a = VipProduct::field('
            goods_id,goods_name,category_id,brand_id,goods_type,
            market_price,price,shipping_fee,clicks,sales,evaluates,
            keywords,state,sort,picture,img_id_array,
            description,create_time,update_time,goods_spec_format
        ')
        ->append(['doc_id','data_source'])
        ->with(['skus' => function($query){
            $query->field('sku_id,goods_id,sku_name,attr_value_items,market_price,price,stock');
        }])
        ->chunk(100, function ($products) use($productElastic){
            if(!$products->count()){
                Log::error('同步失败');
                exit('同步失败');
            }
            Log::info(sprintf('正在同步 ID 范围为 %s 至 %s 的商品', $products->first()->goods_id, $products->last()->goods_id));
            $productElastic->bulkDoc($products->toArray());
            Log::info(sprintf('ID 范围为 %s 至 %s 的商品同步成功', $products->first()->goods_id, $products->last()->goods_id));
        });
        Log::info('同步VIP商城数据结束');
    }
}
